package com.alipay.alipayDemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.alipayDemo.bean.AlipayBean;
import com.alipay.alipayDemo.service.PayService;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 功能描述: 订单接口
 *
 * @author lixiaoxiao
 * @date 2022/09/20 23:44
 * @since JDK 1.8
 */
@RestController()
@RequestMapping("order")
public class OrderController {
    @Resource
    private PayService payService;

    /**
     * 功能描述:  支付宝支付
     *
     * @param outTradeNo  订单号
     * @param subject     主题
     * @param totalAmount 总价
     * @param body        主题
     * @return {@link String}
     * @author lixiaoxiao
     * @date 2022/09/20 23:44
     * @since JDK 1.8
     */
    @PostMapping(value = "alipay")
    public String alipay(String outTradeNo, String subject, StringBuffer totalAmount, String body) throws AlipayApiException {
        return payService.aliPay(AlipayBean.builder()
                .body(body)
                .outTradeNo(outTradeNo)
                .totalAmount(totalAmount)
                .subject(subject).build());
    }

    /**
     * 功能描述: 退款
     *
     * @author lixiaoxiao
     * @date 2022/09/20 14:14
     * @since JDK 1.8
     */
    @PostMapping(value = "alipayRefund")
    public String alipayRefound(@RequestParam("tradeNo") String tradeNo, @RequestParam("refoundNo") String refoundNo, @RequestParam("refundAmount") String refundAmount) throws AlipayApiException {
        return payService.alipayRefound(AlipayBean.builder()
                .tradeNo(tradeNo)
                .outRequestNo(refoundNo)
                .refundAmount(refundAmount)
                .build());
    }
}
